# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time	: 2024/3/28 22:51
# @Author  : jize.liu
# @File	: 战损替换.py
# @声明	 : 版权由 刘继泽 所有,如有疑问，需要帮助请联系刘继泽
import maya.cmds as cmds
import os
locals = "C:/Users/jize.liu/Desktop/EP03-04"

Chrs = ["Z:/AnimationSeries/A10/Assets/Chr/C0090ZengSS/RIG/A10_C0090ZengSS_RIG.ma" , "Z:/AnimationSeries/A10/Assets/Chr/C0020LXQ/RIG/A10_C0020LXQ_RIG.ma" ,
		"Z:/AnimationSeries/A10/Assets/Chr/C0210SanYLH/RIG/A10_C0210SanYLH_RIG.ma" , "Z:/AnimationSeries/A10/Assets/Chr/C0100LinJY/RIG/A10_C0100LinJY_RIG.ma" ,
		"Z:/AnimationSeries/A10/Assets/Chr/C0500FaXiang/RIG/A10_C0500FaXiang_RIG.ma" , "Z:/AnimationSeries/A10/Assets/Chr/C0110XiaoYC/RIG/A10_C0110XiaoYC_RIG.ma" ,
		"Z:/AnimationSeries/A10/Assets/Chr/C0530YanHong/RIG/A10_C0530YanHong_RIG.ma" , "Z:/AnimationSeries/A10/Assets/Chr/C0510FaShan/RIG/A10_C0510FaShan_RIG.ma"]
old_huangniao = "Z:/AnimationSeries/A10/Assets/Chr/C0220HuangN/RIG/A10_C0220HuangN_RIG.ma"
new_huangniao = "Z:/AnimationSeries/A10/Assets/Chr/C0221HuangN/RIG/A10_C0221HuangN_RIG.ma"
old_xuanshe = "Z:/AnimationSeries/A10/Assets/Chr/C0750HeiShuiXS/RIG/A10_C0750HeiShuiXS_RIG.ma"
new_xuanshe = "Z:/AnimationSeries/A10/Assets/Chr/C0751HeiShuiXS/RIG/A10_C0751HeiShuiXS_RIG.ma"
new_Env = "Z:/AnimationSeries/A10/Assets/Env/S1761HuaHaiZBPS/MODlow/A10_S1761HuaHaiZBPS_MODlow.ma"

for local_file in os.listdir (locals) :
	cmds.file (os.path.join (locals , local_file) , o = 1 , f = 1 , options = "v=0;" , ignoreVersion = 1 , typ = "mayaAscii")
	refFile_Node = []
	refNode = cmds.ls (type = "reference")
	refFiles = cmds.file (q = True , r = True)

	for refFile in refFiles :
		for Chr in Chrs :
			if Chr in cmds.file (q = True , r = True) :  # 检查字符文件是否在引用文件列表中
				Namespace = cmds.referenceQuery (Chr , namespace = True).split (":")[1]
				control_rig_node = Namespace + ":CONTROL_RIG_maya2020"

				if cmds.objExists (control_rig_node) :
					#if cmds.attributeQuery ("ClothZhanSun" , node = control_rig_node , exists = True) :
					key = cmds.keyframe (control_rig_node, q = 1)
					if key :
						cmds.cutKey (control_rig_node, t = (key[0] , key[-1]))
					if cmds.attributeQuery ("ClothZhanSun" , node = control_rig_node , exists = True) :
						cloth_attr = control_rig_node + ".ClothZhanSun"
						cmds.setAttr (cloth_attr , 1)
					if cmds.attributeQuery ("BodyZhanSun" , node = control_rig_node , exists = True) :
						cloth_attr = control_rig_node + ".BodyZhanSun"
						cmds.setAttr (cloth_attr , 1)
					print("11111111111111111111")

			if old_xuanshe == refFile :
				node = cmds.referenceQuery (refFile , referenceNode = True)
				cmds.file (new_xuanshe , loadReference = node , type = "mayaAscii" , options = "v=0;")
				if cmds.objExists (control_rig_node) :
					if cmds.attributeQuery ("BodyZhanSun" , node = control_rig_node , exists = True) :
						key = cmds.keyframe (control_rig_node, q = 1)
						if key :
							cmds.cutKey (control_rig_node , t = (key[0] , key[-1]))
						cloth_attr = control_rig_node + ".BodyZhanSun"
						cmds.setAttr (cloth_attr , 1)
						print("22222222222222222222")
			if old_huangniao == refFile :
				node = cmds.referenceQuery (old_huangniao , referenceNode = True)
				cmds.file (new_huangniao , loadReference = node , type = "mayaAscii" , options = "v=0;")
				print("333333333333333")

			'''
			#替换场景
			if "Env" in refFile :
				cmds.file (refFile , removeReference = 1)
			'''
			# cmds.file (new_Env , r = 1 , type = "mayaAscii" , ignoreVersion = 1 , gl = 1 , mergeNamespacesOnClash = 0 , namespace = "A10_S1761HuaHaiZBPS_MODlow" , options = "v=0;")

	cmds.currentTime( 116)
	cmds.currentTime( 106)
	cmds.file (save = True , f = 1 )
	print("444444444444444")

